using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Serialization;
using System.IO;
using Sixeyed.CacheAdapter.NCacheExpressProvider.Tests.Properties;

namespace Sixeyed.CacheAdapter.NCacheExpressProvider.Tests.Fixtures
{
    [TestClass]
    public class NCacheExpressProviderConfigurationTest
    {
        [TestMethod]
        public virtual void Properties()
        {
            string cacheId = "Sixeyed.CacheAdapter.NCacheExpressProvider.Tests";
            TimeSpan defaultLifespan = new TimeSpan(0, 10, 0);

            NCacheExpressProviderConfiguration config = new NCacheExpressProviderConfiguration();
            config.CacheId = cacheId;
            config.DefaultLifespan = defaultLifespan;

            Assert.AreEqual<string>(cacheId, config.CacheId);
            Assert.AreEqual<double>(defaultLifespan.TotalMinutes, config.DefaultLifespan.TotalMinutes);
        }

        [TestMethod]
        public virtual void Deserialize()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(NCacheExpressProviderConfiguration));
            StringReader reader = new StringReader(Resources.SampleConfig);
            NCacheExpressProviderConfiguration config = (NCacheExpressProviderConfiguration) serializer.Deserialize(reader);

            Assert.AreEqual<string>("SampleConfig", config.CacheId);
            Assert.AreEqual<double>(10, config.DefaultLifespan.TotalMinutes);
        }

        [TestMethod]
        public virtual void Serialize()
        {            
            NCacheExpressProviderConfiguration config = new NCacheExpressProviderConfiguration();
            config.CacheId = "Sample";
            //config.DefaultLifespan = new TimeSpan(0, 20, 0);

            XmlSerializer serializer = new XmlSerializer(typeof(NCacheExpressProviderConfiguration));
            MemoryStream stream = new MemoryStream();
            serializer.Serialize(stream, config);

            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            string xml = reader.ReadToEnd();

            Assert.IsFalse(string.IsNullOrEmpty(xml));
        }
    }
}
